<?php

namespace app\manage\controller;

use app\common\validate\merchant\RechargeLogValid;
use app\manage\loader\ManageServices;
use think\Exception;

/**
 *  商户线下充值
 */
class Recharge extends BaseApi
{


    /**
     * 格式化where条件
     * @param $getData
     * @return array
     */
    protected function formatWhere($getData): array
    {
        $where = [];
        if (isset($getData['mer_id']) && ($merId = trim($getData['mer_id'])) !== '') {
            $where[] = ['a.mer_id', '=', $merId];
        }

        if (isset($getData['pay_status']) && ($payStatus = trim($getData['pay_status'])) !== '') {
            $where[] = ['a.pay_status', '=', $payStatus];
        }
        if (!empty($getData['pay_type']) && ($payType = trim($getData['pay_type'])) !== '') {
            $where[] = ['a.pay_type', '=', $payType];
        }
        if (!empty($payOrder = trim($getData['pay_order']))) {
            if ($payOrder[0] !== 'm') {
                $where[] = ['a.out_pay_order', '=', $payOrder];
            } else {
                $where[] = ['a.pay_order', '=', $payOrder];
            }
        }
        $stime = trim($getData['stime']);
        $etime = trim($getData['etime']);
        if (!empty($stime) && !empty($etime)) {
            $where[] = ['a.create_at', 'between', [$stime . ' 00:00:00', $etime . ' 23:59:59']];
        } else {
            if (!empty($stime)) {
                $where[] = ['a.create_at', '>=', $stime];
            }
            if (!empty($etime)) {
                $where[] = ['a.create_at', '<=', $etime];
            }
        }
        return $where;
    }

    /**
     * 待充值记录
     * @return void
     */
    public function page(): void
    {
        $getData = input('get.');
        try {
            $option = $this->formatOption($getData);
            $option['where'] = $this->formatWhere($getData);
            $list = ManageServices::Recharge()->balanceLog($option);
            api_result(API_SUCCESS, '操作成功', $list);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 支付单详情
     * @return void
     */
    public function info(): void
    {
        $getData = input('get.');
        try {

            $payOrder = trim($getData['pay_order']);
            if (empty($payOrder)) {
                api_result(API_ERROR, '支付单号不能为空');
            }
            $option['where'][] = ['pay_order' => $payOrder];
            $info = ManageServices::Recharge()->rechargeInfo($option);
            api_result(API_SUCCESS, '操作成功', $info);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 线下支付
     * @return void
     */
    public function offline_pay(): void
    {
        $postData = input('post.');
        try {
            validate(RechargeLogValid::class)->scene('offline_pay')->check($postData);
            ManageServices::Recharge()->offline_pay($postData['pay_order'], $postData['real_money'], $this->opUser, $postData['memo'],);
            api_result(API_SUCCESS, '支付成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }
}